package com.initialize.core.security.handler;


import cn.hutool.json.JSONObject;
import lombok.SneakyThrows;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2020/1/5
 * Time: 5:17 下午
 * 登录账号密码错误等情况下,会调用的处理类
 */
@Component
public class BaseAuthenticationFailHandler implements AuthenticationFailureHandler {


    @SneakyThrows
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 1002);
        jsonObject.put("msg", "账号密码错误");

        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().print(jsonObject.toString());
    }
}